package dao;


import java.util.List;

import com.db4o.ObjectContainer;

import model.SparePart;

public class SparePartDao {
//	private List<SparePart> spareParts = new ArrayList<SparePart>();

	private static SparePartDao instance = null;

	public SparePartDao() {

	}

	/**
	 * This is a static method that return new SparePartDao when instance is
	 * null
	 * 
	 * @author Osahon Idehen
	 */

	public static SparePartDao getInstance() {
		if (instance == null)
			instance = new SparePartDao();
		return instance;
	}

	// /**
	// * This adds a sparePart
	// * @author Osahon Idehen
	// */
	//	
	// public void addSparePart(SparePart sparePart) {
	// if (!spareParts.contains(sparePart)) {
	// spareParts.add(sparePart);
	// }
	// }
	//
	// /**
	// * This removes a sparePart
	// * @author Osahon Idehen
	// */
	//	
	// public void removeSparePart(SparePart sparePart) {
	// if (spareParts.contains(spareParts)) {
	// spareParts.remove(sparePart);
	// }
	// }
	//
	// /**
	// * This return spareParts from the list
	// * @author Osahon Idehen
	// */
	//	
	// public List<SparePart> getSpareParts() {
	// return spareParts;
	// }

	// TODO DB4O methods-start
	/**
	 * Link to the database.
	 * 
	 * @author Csaba Tarkanyi
	 */
	private ObjectContainer db = DaoManager.getInstance().getObjectContainer();

	/**
	 * Adds a SparePart to the database.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void addSparePart(SparePart sparePart) {
		db.set(sparePart);
		db.commit();
	}

	/**
	 * Removes a SparePart from the database.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void removeSparePart(SparePart sparePart) {
		db.delete(sparePart);
		db.commit();
	}

	/**
	 * Returns a List of SpareParts from the database.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<SparePart> getSpareParts() {
		return db.get(SparePart.class);
	}

	/**
	 * Returns the SparePart with the id. If no SparePart exists in the database
	 * with the id, null is returned.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public SparePart getSparePartByUniqueID(long id) {
		SparePart proto = new SparePart(id, 0, 0, 0, "", 0, 0, 0, "");
		List<SparePart> spareParts = db.get(proto);
		if (spareParts.size() > 0)
			return spareParts.get(0);
		else
			return null;
	}

	// TODO DB4O methods-end
}